package org.rockwalrus.ruly;

import static java.util.Calendar.*;

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.TimeZone;

public class Time {
    static final TimeZone TIME_ZONE = java.util.TimeZone.getTimeZone("America/New_York");
    
    public static boolean isGameDay(Calendar time) {
        int day = time.get(DAY_OF_WEEK);
        
        return day != SATURDAY && day != SUNDAY;
    }
    
    static public long addGameHours(long time, int hours) {
        Calendar deadline = new GregorianCalendar(TIME_ZONE);
        deadline.setTimeInMillis(time);
        
        if (!isGameDay(deadline)){
            deadline.add(DATE, 1);
            deadline.set(HOUR_OF_DAY, 0);
            deadline.set(MINUTE, 0);
            deadline.set(SECOND, 0);
            deadline.set(MILLISECOND, 0);
            
        }
        while (hours > 0 || !isGameDay(deadline))
            if (!isGameDay(deadline)) {
                deadline.add(DATE, 1);
            } else {
                int increment = Math.min(hours, 24);

                deadline.add(HOUR_OF_DAY, increment);
                hours -= increment;
            }
        
        return deadline.getTimeInMillis();
    }

    
}
